package util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 测试类，测试数据库连接是否成功
 */
public class DatabaseConnectionTest implements Validator {

    private final static Logger logger = LoggerFactory.getLogger(DatabaseConnectionTest.class);
    public static void main(String[] args)  throws IOException {

        //加载配置文件

        ApplicationContext applicationContext =new ClassPathXmlApplicationContext("spring/spring-dao.xml");

        //获取jdbcTemplate实例
        JdbcTemplate jdbcTemplate=(JdbcTemplate) applicationContext.getBean("jdbcTemplate");

        //使用execute()方法执行SQL语句，创建用户账户管理表account

        jdbcTemplate.execute("create table account("+
                "id int primary key auto_increment,"+
                "usrname varchar(50),"+
                "balance double)");
        System.out.println("账户表创建成功!");

        Logger log = LoggerFactory.getLogger("logback.xml");
        log.debug("你好");
        logger.isDebugEnabled();
        logger.debug("ceshi");
      //  ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");
//        logger.info("测试数据库连接");
//        String resource = "mybatis-config.xml";
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory =  new SqlSessionFactoryBuilder().build(inputStream);
//        //3.通过回话工厂得到sqlsession 对象
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        User user = new User();
//        user.setId(3);
//        user.setName("闫有泉");
//        user.setAge(24);
//        sqlSession.insert("mapper.UserMapper.insertOne",user);
//        sqlSession.commit();
        //4.通过sqlsession操作数据库，sqlsession
//        List<User> lists= sqlSession.selectList("mapper.UserMapper.findAllUser");
//        System.out.println(lists.size());
//        for(User emp:lists){
//            logger.info(emp.toString());
//        }
        //关闭sqlsession对象
//        sqlSession.close();/
        ApplicationContext ctx = new ClassPathXmlApplicationContext("mybatis-config.xml");

        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:mybatis-config.xml");


    }


    public  static void test(int a){

        StringBuffer buffer = new StringBuffer();
        buffer.append("<a>"+ a +"</a>");
        System.out.println(buffer.toString());
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return false;
    }

    @Override
    public void validate(Object o, Errors errors) {

    }

    private static void release(AutoCloseable ac){
        if(ac != null){
            try {
                ac.close();
            }catch (Exception e){

            }

        }

    }
}
